package com.suanfa.dp;

/**
 * 最大乘积子数组
 * https://leetcode.cn/problems/maximum-product-subarray/
 */
public class MaxProductArray {

    public static void main(String[] args) {
        int[] arr = {-2};
        System.out.println(maxProduct(arr));
    }

    public static int maxProduct(int[] arr){
        int max = arr[0], min = arr[0];
        int ans = arr[0];
        for(int i=1; i< arr.length; i++){
          int mx = Math.max(arr[i], Math.max(max * arr[i], min * arr[i]));
          int mn = Math.min(arr[i], Math.min(max * arr[i], min * arr[i]));
          ans = Math.max(ans, Math.max(mx, mn));
          max = mx;
          min = mn;
        }
        return ans;

    }
}
